#!/bin/bash

#cd "$(dirname "$0")"是为了进入到脚本所在目录下 
source "$(
    cd "$(dirname "$0")" || exit 1
    pwd
)/./common.sh"

#下载依赖和工具包
function install_development() {
    yum install -y wget patch cmake
    wget -O /etc/yum.repos.d/openEulerOS.repo https://repo.huaweicloud.com/repository/conf/openeuler_aarch64.repo
    yum clean all
    yum makecache
    yum install -y libomp suitesparse suitesparse-devel libtirpc-devel python3-devel eigen3-devel boost boost-devel swig
    /usr/bin/python3 -m pip install pytest
    rm -rf eigen-3.4.0.tar.gz
    rm -rf "$1"/eigen
    mkdir "$1"/eigen
    wget -P "$1"/eigen https://gitlab.com/libeigen/eigen/-/archive/3.4.0/eigen-3.4.0.tar.gz
    cd "$1"/eigen
    tar -zvxf eigen-3.4.0.tar.gz
    cd eigen-3.4.0
    mkdir build && cd build
    cmake \
    -DCMAKE_C_COMPILER=$2/bisheng-compiler-2.1.0/bin/clang \
    -DCMAKE_CXX_COMPILER=$2/bisheng-compiler-2.1.0/bin/clang++ \
    -DCMAKE_C_FLAGS="-march=armv8.2-a  -fsigned-char -ffp-contract=off" \
    -DCMAKE_CXX_FLAGS="-march=armv8.2-a  -fsigned-char -ffp-contract=off" \
    ..
    make && make install
}

#获取apbs
function download_apbs() {
    echo "# apbs正在下载"
    rm -rf "$1"/apbs
    mkdir "$1"/apbs
    git clone https://github.com/Electrostatics/apbs.git "$1"/apbs
    if [[ $? -eq 0 ]]; then
            echo -e "\033[1;32;1m# apbs已经克隆成功\033[0m"
    else
            echo -e "\033[31m# apbs下载失败,请重新执行脚本\033[0m"
            exit 
    fi
    cd "$1"/apbs
    git checkout f24dd7629a41e253287bbb643589cd2afb776484
    if [[ $? -eq 0 ]]; then
            echo -e "\033[1;32;1m# 版本控制成功\033[0m"
    else
            echo -e "\033[31m# 版本控制失败,请重新执行脚本\033[0m"
            exit 1
    fi
    echo -e "\033[1;32;1m# 正在克隆apbs子模块\033[0m"
    git submodule init
    git submodule update
    if [[ $? -eq 0 ]]; then
            echo -e "\033[1;32;1m# apbs子模块已经克隆成功\033[0m"
    else
            echo -e "\033[31m# apbs子模块下载失败,请重新执行脚本\033[0m"
            exit 1
    fi
}

function install_apbs() {
    echo -e "\033[1;32;1m# apbs开始安装\033[0m"
    cp "$3"/apbs-3.4.1.patch "$1"/apbs
    cd "$1"/apbs
    export LD_LIBRARY_PATH=/usr/lib64:$LD_LIBRARY_PATH
    export LD_LIBRARY_PATH=/lib64:$LD_LIBRARY_PATH
    patch -p1 <./apbs-3.4.1.patch
    echo -e "\033[1;32;1m# 正在安装子模块\033[0m"
    cd "$1"/apbs/externals/pybind11
    mkdir build
    cd build
    cmake \
    -DCMAKE_C_COMPILER=$2/bisheng-compiler-2.1.0/bin/clang \
    -DCMAKE_CXX_COMPILER=$2/bisheng-compiler-2.1.0/bin/clang++ \
    -DCMAKE_C_FLAGS="-march=armv8.2-a  -fsigned-char -ffp-contract=off" \
    -DCMAKE_CXX_FLAGS="-march=armv8.2-a  -fsigned-char -ffp-contract=off" \
    ..
    if [[ $? -eq 0 ]]; then
            echo -e "\033[1;32;1m# apbs子模块cmake成功\033[0m"
    else
            echo -e "\033[31m# apbs子模块安装失败,请重新执行脚本\033[0m"
            exit 1
    fi
    make && make install
    if [[ $? -eq 0 ]]; then
            echo -e "\033[1;32;1m# apbs子模块已经安装成功\033[0m"
    else
            echo -e "\033[31m# apbs子模块安装失败,请重新执行脚本\033[0m"
            exit 1
    fi

    cd "$1"/apbs
    echo -e "\033[1;32;1m# 正在安装apbs\033[0m"
    mkdir build
    cd build
    export APBS_BUILD_DIR="$1"
    export INSTALL_DIR="$2"/apbs
    export PATH="$INSTALL_DIR"/bin:$PATH
    cmake \
    -DCMAKE_INSTALL_PREFIX=$INSTALL_DIR \
    -DENABLE_GEOFLOW=ON \
    -DENABLE_OPENMP=ON \
    -DENABLE_PYTHON=ON \
    -DENABLE_TESTS=ON \
    -DBUILD_SHARED_LIBS=ON \
    -DENABLE_BEM=OFF \
    -DENABLE_PBAM=OFF \
    -DENABLE_PBSAM=OFF \
    -DCMAKE_C_COMPILER=$2/bisheng-compiler-2.1.0/bin/clang \
    -DCMAKE_CXX_COMPILER=$2/bisheng-compiler-2.1.0/bin/clang++ \
    -DCMAKE_C_FLAGS="-march=armv8.2-a  -fsigned-char -ffp-contract=off" \
    -DCMAKE_CXX_FLAGS="-march=armv8.2-a  -fsigned-char -ffp-contract=off" \
    -DPython3_EXECUTABLE=$(which python3) \
    ..
    if [[ $? -eq 0 ]]; then
            echo -e "\033[1;32;1m# apbs编译成功\033[0m"
    else
            echo -e "\033[31m# apbs编译失败,请重新执行脚本\033[0m"
            exit 1
    fi
    make && make install
    if [[ $? -eq 0 ]]; then
            echo -e "\033[1;32;1m# apbs已经安装成功\033[0m"
    else
            echo -e "\033[31m# apbs安装失败,请重新执行脚本\033[0m"
            exit 1
    fi
    export LD_LIBRARY_PATH=$INSTALL_DIR/lib64:$LD_LIBRARY_PATH
}

function cp_apbs() {
  mkdir -p "$2"/apbs
  cp -r "$1"/apbs/* "$2"/apbs
}

# 创建modulefiles文件
function write_apbs_module() {
  cat >"$1/apbs/apbs_modulefiles"<<EOF
#%Module1.0
conflict apbs
variable modfile [file normalize [info script]]
proc getModulefileDir {} {
    variable modfile
    set modfile_path [file dirname \$modfile]
    return \$modfile_path
}
set pwd [getModulefileDir]
set apbs \$pwd
setenv apbs \$apbs
prepend-path PATH \$apbs/bin
prepend-path LD_LIBRARY_PATH /usr/lib64
prepend-path LD_LIBRARY_PATH /lib64
prepend-path LD_LIBRARY_PATH \$apbs/lib64
prepend-path LD_LIBRARY_PATH \$apbs/lib
EOF
}

function main() {
  is_check_build_install_directory "$1" "$2" "apbs-3.4.1.sh"
  is_equal_build_install_path "$1" "$2"
  install_development "$1" "$2"
  download_apbs "$1"
  install_apbs "$1" "$2" "$3"
  cp_apbs "$1" "$2"
  write_apbs_module "$2"
  rm -rf "$1"/apbs
}

main "$@"
